package com.zxl.always.on.display.constant;

import android.content.Intent;

import com.zxl.always.on.display.ApplicationHelper;
import com.zxl.always.on.display.ui.service.ScreenService;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;

/**
 * 应用锁设置
 * Created by makai on 15-6-10.
 */
public class LockerSetting {

    /**
     * 开关
     */
    @SettingsDataSave.Settings(name = ISharedPreferences.SETTINGS_SWITCH_STATUS)
    public static boolean sIsSwitchStatus = false;

    @SettingsDataSave.Settings(name = ISharedPreferences.SETTINGS_SHOW_TIMER_SETTING)
    public static boolean sShowTimerSetting = true;

    @SettingsDataSave.Settings(name = ISharedPreferences.SETTINGS_START_HOUR)
    public static String sStartHour = "07";

    @SettingsDataSave.Settings(name = ISharedPreferences.SETTINGS_START_MIN)
    public static String sStartMin = "00";

    @SettingsDataSave.Settings(name = ISharedPreferences.SETTINGS_END_HOUR)
    public static String sEndHour = "21";

    @SettingsDataSave.Settings(name = ISharedPreferences.SETTINGS_END_MIN)
    public static String sEndMin = "00";

    /**
     * 初始化设置数据
     */
    public static void init() {
        SettingsDataSave.initData(LockerSetting.class, ISharedPreferences.SP_DEFAULT_MULTI_PROCESS);
    }

    /**
     * 保存应用锁数据
     * @param key
     * @param value
     */
    public static void saveLockerSettings(String key, Object value) {
        SettingsDataSave.saveData(LockerSetting.class, ISharedPreferences.SP_DEFAULT_MULTI_PROCESS, key, value);
        notifyService(key);
    }

    public static boolean saveLockerSettings(Map<String, Object> values) {
        if (SettingsDataSave.saveData(LockerSetting.class, ISharedPreferences.SP_DEFAULT_MULTI_PROCESS, values)) {
            Set<String> set = values.keySet();
            for (String value : set) {
                notifyService(value);
            }
            return true;
        }
        return false;
    }

    private static void notifyService(String key) {
        Field[] fields = LockerSetting.class.getFields();
        for (Field field : fields) {
            SettingsDataSave.Settings annotation = field.getAnnotation(SettingsDataSave.Settings.class);
            if (null != annotation && annotation.name().equals(key)) {
                Intent intent = new Intent(ApplicationHelper.getContext(), ScreenService.class);
                intent.setAction(ICustomAction.ACTION_REFRESH_SETTINGS);
                ApplicationHelper.getContext().startService(intent);
            }
        }
    }

}
